package client.comm.commlib.widget

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.util.Log
import android.view.LayoutInflater
import android.widget.FrameLayout
import androidx.databinding.DataBindingUtil
import client.comm.commlib.R
import client.comm.commlib.databinding.ViewErrorLayoutBinding
import client.comm.commlib.utils.Util

class EmptyLayout @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {
    val binding: ViewErrorLayoutBinding

    companion object {

        val NETWORK_ERROR = 1
        val NETWORK_LOADING = 2
        val NODATA = 3
        val HIDE_LAYOUT = 4
        val NODATA_ENABLE_CLICK = 5
    }

    init {
        binding = DataBindingUtil.inflate(
            LayoutInflater.from(context),
            R.layout.view_error_layout,
            this,
            true
        )

        binding.root.setOnClickListener {
            if (errorType != NETWORK_LOADING)
                click.invoke()
        }
    }

    override fun setOnClickListener(l: OnClickListener?) {
        super.setOnClickListener(l)
        l?.let {
            click = {
                errorType = (NETWORK_LOADING)
                it.onClick(this)
            }
        }
    }

    var click: () -> Unit = {
        Log.e("ee", "eee")
    }

    var errorType: Int = NETWORK_LOADING
        set(i) {
            visibility = VISIBLE
            when (i) {
                NETWORK_ERROR -> {

                    // img.setBackgroundDrawable(SkinsUtil.getDrawable(context,"pagefailed_bg"));
                    if (Util.hasInternet()) {
                        binding.tvErrorLayout.setText(R.string.error_click_to_refresh)
                        binding.imgErrorLayout.setBackgroundResource(R.mipmap.ic_tip_fail)
                    } else {
                        binding.tvErrorLayout.setText(R.string.error_view_network_error_click_to_refresh)
                        binding.imgErrorLayout.setBackgroundResource(R.mipmap.page_icon_network)
                    }
                    binding.imgErrorLayout.setVisibility(VISIBLE)

                    binding.animProgress.hide()
                }
                NETWORK_LOADING -> {
                    binding.imgErrorLayout.setVisibility(GONE)
                    binding.animProgress.show()
                    binding.tvErrorLayout.setText("加载中...")
                }
                NODATA -> {
                    binding.imgErrorLayout.setVisibility(VISIBLE)
                    binding.tvErrorLayout.setText(R.string.error_view_no_data)
                    binding.imgErrorLayout.setBackgroundResource(R.mipmap.ic_tip_empty)

                    binding.animProgress.hide()
                }
                HIDE_LAYOUT -> {
                    binding.animProgress.hide()
                    visibility = GONE
                }
                NODATA_ENABLE_CLICK -> {
                    binding.imgErrorLayout.setBackgroundResource(R.mipmap.page_icon_empty)
                    // img.setBackgroundDrawable(SkinsUtil.getDrawable(context,"page_icon_empty"));
                    binding.imgErrorLayout.setVisibility(VISIBLE)
                    binding.animProgress.hide()
                    binding.tvErrorLayout.setText(R.string.error_view_no_data)

                }


            }
            field = i
        }
}